﻿using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace Catequese.Classes
{
    class Catequistas
    {
        private int _id;
        private string _username;
        private string _password;
        private string _nome;

        public int Id
        {
            get
            {
                return _id;
            }
        }

        public string Username
        {
            get
            {
                return _username;
            }

            set
            {
                _username = value;
            }
        }

        public string Password
        {
            set
            {
                Criptografia.Criptografia cripto = new Criptografia.Criptografia();
                _password = cripto.HashstringMd5(value);
            }
        }

        public string Nome
        {
            get
            {
                return _nome;
            }

            set
            {
                _nome = value;
            }
        }

        public bool Autenticar()
        {
            BaseDados bd = new BaseDados();

            string strQuery = "SELECT * FROM catequistas WHERE username = @username AND password = @password AND estado = 1";
            bd.myCommand = new MySqlCommand(strQuery, bd.myConnection);
            bd.myCommand.Parameters.Add("@username", MySqlDbType.VarChar).Value = _username;
            bd.myCommand.Parameters.Add("@password", MySqlDbType.VarChar, 32).Value = _password;

            try
            {
                bd.myConnection.Open();
                bd.myReader = bd.myCommand.ExecuteReader();

                while (bd.myReader.Read())
                {                   
                    return true;
                }

                bd.myReader.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Ocorreu um erro: " + ex.Message);
            }
            finally
            {
                bd.myConnection.Close();
            }

            return false;
        }
    }
}
